package net.anjingnorak.javabase.connection;

import java.sql.ResultSet;

import net.anjingnorak.javabase.model.DataTable;
 

/**
 * @author Gian Rishandy
 * 
 * Memiliki funsi untuk membuat query atau storeprocedure dengan kembalian berupa sql script command ataupun resultset
 *   
 */
public class Command {
	private String query;
	private String storeProcedure;
	private String[] parameterName;
	private Object[] parameterValue;
	
	/**
	 * Mengembalikan dalam bentuk sql script
	 * @return String
	 */
	public String getSQLScriptInCommand(){
		String script = "";
		
		if(query != null){
			script = query;
		}else{
			if(Connection.getConnectionType().equalsIgnoreCase(Connection.MYSQL)){
				script = "Call " + storeProcedure + "(";
				
				if(parameterValue != null){
					for(int i=0;i<parameterValue.length;i++){
						if(i == 0){
							if(parameterValue[i] == null){
								script = script + null;
							}else{
								script = script + "'" + parameterValue[i] + "'";
							}
						}else{
							if(parameterValue[i] == null){
								script = script + ", " + null;
							}else{
								script = script + ", '" + parameterValue[i] + "'";
							}
						}
					}
				}
				
				script = script + ")";
			}else if(Connection.getConnectionType().equalsIgnoreCase(Connection.SQLSERVER)){
				script = "exec " + storeProcedure + " ";
				
				if(parameterValue != null && parameterName != null){
					for(int i=0;i<parameterValue.length;i++){
						if(i == 0){
							script = script + parameterName[i] + "='" + parameterValue[i] + "'";
						}else{
							script = script + ", " + parameterName[i] + "='" + parameterValue[i] + "'";
						}
					}
				}
			}
		}
		
		return script;
	}
	
	/**
	 * Meng-execute sql script dengan pengembalian berupa resultset
	 * @return Result Set
	 * @throws Exception
	 */
	public ResultSet executeQuery() throws Exception{
		return SQLExecution.executeQuery(getSQLScriptInCommand());
	}
	
	/**
	 * Meng-execute sql script dengan pengembalian berupa boolean false = gagal dan true = berhasil
	 * @return boolean
	 * @throws Exception
	 */
	public boolean executeUpdate() throws Exception{
		return SQLExecution.executeUpdate(getSQLScriptInCommand());
	}
	
	
	/**
	 * Meng-execute sql script dengan pengembalian berupa datatable
	 * @return Datatable
	 * @throws Exception
	 */
	public DataTable executeDatatable() throws Exception{
		DataTable datatable = new DataTable();
		datatable.setDatatable(executeQuery());
		return datatable;
	}
	
	public String getQuery() {
		return query;
	}
	public void setQuery(String query) {
		this.query = query;
	}
	public String getStoreProcedure() {
		return storeProcedure;
	}
	public void setStoreProcedure(String storeProcedure) {
		this.storeProcedure = storeProcedure;
	}
	public String[] getParameterName() {
		return parameterName;
	}
	public void setParameterName(String[] parameterName) {
		this.parameterName = parameterName;
	}
	public Object[] getParameterValue() {
		return parameterValue;
	}
	public void setParameterValue(Object[] parameterValue) {
		this.parameterValue = parameterValue;
	}
}
